# 导入所需的库
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# 读取 CSV 文件
df = pd.read_csv('C:/Users/Administrator/Desktop/car_prices.csv')
#  设置图片清晰度
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 300

# 设置字体为WenQuanYi Zen Hei，用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['WenQuanYi Zen Hei']

# 查看实际销售价和平均零售价的分布
df.plot.box(subplots = True, sharey = False, figsize = (8, 5))
fig = plt.gcf()

# 显示图形
plt.show()
plt.close()

# 查看价差的分布，并用图形表示出来
(df['mmr']-df['sellingprice']).plot.hist(bins = 50, title = '价差的分布', xlabel = '价差')

# 计算第三四分位数和第一四分位数的价差
third_quantile = df['mmr'].quantile(0.75) - df['sellingprice'].quantile(0.75)
first_quantile = df['mmr'].quantile(0.25) - df['sellingprice'].quantile(0.25)

# 计算中位数价差
median_value = df['mmr'].median() - df['sellingprice'].median()

# 计算四分位距
IQR = third_quantile - first_quantile

# 计算上限和下限
upper_bound = third_quantile + 1.5 * IQR
lower_bound = first_quantile - 1.5 * IQR